package online.longzipeng.householdbillssystem.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import online.longzipeng.householdbillssystem.business.dto.UserLoginResponseDTO;
import online.longzipeng.householdbillssystem.business.dto.user.UserQueryResponseDTO;
import online.longzipeng.householdbillssystem.business.entity.User;
import online.longzipeng.householdbillssystem.business.vo.querey.QueryVO;
import online.longzipeng.householdbillssystem.business.vo.user.UserLoginVO;
import online.longzipeng.householdbillssystem.business.vo.user.UserSaveVO;
import online.longzipeng.householdbillssystem.business.vo.user.UserUpdateVO;
import online.longzipeng.householdbillssystem.common.result.ResponseEnum;
import online.longzipeng.householdbillssystem.common.result.Result;
import online.longzipeng.householdbillssystem.common.utils.DateUtils;
import online.longzipeng.householdbillssystem.common.utils.ExceptionUtils;
import online.longzipeng.householdbillssystem.common.utils.ResponseUtils;
import online.longzipeng.householdbillssystem.config.JWTConfig;
import online.longzipeng.householdbillssystem.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.ResultSet;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-17
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户相关接口",description = "用户相关接口",tags = "用户相关接口")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JWTConfig jwtConfig;

    @PostMapping("/get-list-page")
    @ApiOperation("查询所有用户带分页")
    public Result getListPage(@RequestBody QueryVO queryVO){
        IPage<UserQueryResponseDTO> users = userService.getListPage(queryVO);
        return Result.success(users);
    }

    @GetMapping("/find_user_exists_by_userName")
    @ApiOperation("查询用户名是否已存在")
    public Result findUserExistsByUserName(String userName){
        userService.findUserExistsByUserName(userName);
        Result result = Result.success();
        result.setMessage("该用户名可以使用!");
        result.setData(true);
        return result;
    }

    @PostMapping("/save")
    @ApiOperation("新建保存用户信息")
    public Result save(@Valid @RequestBody UserSaveVO userSaveVO){
        User user = new User();
        BeanUtils.copyProperties(userSaveVO,user);
        ExceptionUtils.isTrue(userService.add(user),"用户保存失败！！请检查填写的用户信息",user);
        if (log.isDebugEnabled()){
            log.info(String.format("新建用户成功,用户信息：%s",user.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @DeleteMapping("/delete_one/{id}")
    @ApiOperation("删除单个用户信息")
    public Result deleteOne(@ApiParam(value="用户id",required = true) @PathVariable("id") Integer id){
        ExceptionUtils.isTrue(userService.delete(id),"删除失败！！参数不合法或已被其他用户删除",id);
        if (log.isDebugEnabled()){
            log.info(String.format("用户删除成功,用户信息：%d",id));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PostMapping("/delete")
    @ApiOperation("删除多个用户信息")
    public Result delete(@ApiParam(value="用户id数组",required = true, example = "1,2,3,4") @RequestParam("ids") Integer[] ids){
        for (Integer id : ids) {
            ExceptionUtils.isTrue(userService.delete(id),"删除失败！！参数不合法或已被其他用户删除",ids);
        }
        if (log.isDebugEnabled()){
            log.info(String.format("用户批量删除成功，ids：%s",ids.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PutMapping("/update")
    @ApiOperation("修改用户信息")
    public Result update(@Valid @RequestBody UserUpdateVO userUpdateVO){
        User user = new User();
        BeanUtils.copyProperties(userUpdateVO,user);
        ExceptionUtils.isTrue(userService.edit(user),"更新失败！！请检查输入的数据",user);
        if (log.isDebugEnabled()){
            log.info(String.format("修改用户信息成功，用户信息：%s",user.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    public Result login(@RequestBody UserLoginVO userLoginVO){
        User user = new User();
        BeanUtils.copyProperties(userLoginVO,user);
        User loginUser = userService.login(user);
        // 设置登录用户响应的token
        UserLoginResponseDTO userLoginResponseDTO = new UserLoginResponseDTO();
        BeanUtils.copyProperties(loginUser,userLoginResponseDTO);
        userLoginResponseDTO.setToken(jwtConfig.generateJwt(userLoginResponseDTO));
        if (log.isDebugEnabled()){
            log.info(String.format("用户 %s 登录成功，%s",loginUser.getNick(), DateUtils.DateToString(new Date())));
        }
        return ResponseUtils.successResponse(String.format("亲爱的 \"%s\" 欢迎回来",loginUser.getNick()),userLoginResponseDTO);
    }

}
